package com.yly.doctorapp.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.yly.doctorapp.R;
import com.yly.doctorapp.base.BaseActivity;
import com.yly.doctorapp.base.DoctorAppApplication;
import com.yly.doctorapp.bean.ResponsDataBean;
import com.yly.doctorapp.bean.UserBean;
import com.yly.doctorapp.constant.Configution;
import com.yly.doctorapp.ui.home.fragment.MineFragment;
import com.yly.doctorapp.ui.home.fragment.PrescriptionFragment;
import com.yly.doctorapp.ui.usercenter.LoginActivity;
import com.zhcx.zhcxlibrary.utils.SPUtils;
import com.zhcx.zhcxlibrary.utils.StringUtils;
import com.zhcx.zhcxlibrary.utils.ToastUtils;
import com.zhcx.zhcxlibrary.utils.statusbar.StatusBarUtil;

import org.xutils.common.Callback;
import org.xutils.common.util.LogUtil;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class HomePagerActivity extends BaseActivity {
    @BindView(R.id.iv_chu)
    ImageView ivChu;
    @BindView(R.id.tv_chu)
    TextView tvChu;
    @BindView(R.id.ll_chu)
    LinearLayout llChu;
    @BindView(R.id.iv_mine)
    ImageView ivMine;
    @BindView(R.id.tv_mine)
    TextView tvMine;
    @BindView(R.id.ll_mine)
    LinearLayout llMine;
    private PrescriptionFragment prescriptionFragment;
    private MineFragment mineFragment;
    private List<Fragment> fragmentList = new ArrayList<>();
    private int currentIndex = -1;
    private int index = 0;
    private SPUtils mSputils;

    @Override
    protected int getNaviteColor() {
        return R.color.text_main_color;
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_homepager;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        mSputils = new SPUtils(getApplicationContext());
        prescriptionFragment = new PrescriptionFragment();
        mineFragment = new MineFragment();
        fragmentList.clear();
        fragmentList.add(prescriptionFragment);
        fragmentList.add(mineFragment);
    }

    @Override
    protected void initData() {
        if (!StringUtils.isEmpty(getIntent().getStringExtra("type"))) {
            if (getIntent().getStringExtra("type").equals("0")) {
                initLogin(mSputils.getString(Configution.USERPHONE), mSputils.getString(Configution.USERPASSWORD));
            } else {
                selectItem(0);
            }
        } else {
            selectItem(0);
        }
    }

    private void initLogin(final String phone, final String password) {
        RequestParams params = new RequestParams(Configution.HOST + Configution.LOGIN);
        params.addBodyParameter("telephone", phone);
        params.addBodyParameter("pwd", password);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                LogUtil.d(result);
                if (!StringUtils.isEmpty(result)) {
                    ResponsDataBean responsDataBean = JSON.parseObject(result, ResponsDataBean.class);
                    if (responsDataBean.getCode().equals("1000")) {
                        if (!StringUtils.isEmpty(responsDataBean.getData())) {
                            DoctorAppApplication.mUserBean = JSON.parseObject(responsDataBean.getData(), UserBean.class);
                            selectItem(0);
                        }
                    } else {
                        Intent mIntent = new Intent(HomePagerActivity.this, LoginActivity.class);
                        startActivity(mIntent);
                        finish();
                        ToastUtils.show(getApplicationContext(), responsDataBean.getMsg(), 100);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                ToastUtils.show(getApplicationContext(), "网络连接中断", 100);
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void selectItem(int it) {
        index = it;
        if (currentIndex != index) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            if (currentIndex != -1) {
                ft.hide(fragmentList.get(currentIndex));
            }
            if (!fragmentList.get(index).isAdded()) {
                ft.add(R.id.fl_content, fragmentList.get(index));
            }
            ft.show(fragmentList.get(index)).commitAllowingStateLoss();
            currentIndex = index;
            changeButtomColor(currentIndex);
        }
    }

    private void changeButtomColor(int index) {
        switch (index) {
            case 0:
                ivChu.setImageResource(R.mipmap.homechuselected);
                tvChu.setTextColor(0xffa0524b);
                ivMine.setImageResource(R.mipmap.minenoselect);
                tvMine.setTextColor(0xff666666);
                StatusBarUtil.setColor(HomePagerActivity.this, HomePagerActivity.this.getResources().getColor(R.color.text_main_color), 1);
                break;
            case 1:
                ivChu.setImageResource(R.mipmap.chunoselect);
                tvChu.setTextColor(0xff666666);
                ivMine.setImageResource(R.mipmap.mineselected);
                tvMine.setTextColor(0xffa0524b);
                StatusBarUtil.setColor(HomePagerActivity.this, HomePagerActivity.this.getResources().getColor(R.color.minebg), 1);
                break;
        }
    }

    @OnClick({R.id.ll_chu, R.id.ll_mine})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_chu:
                selectItem(0);
                break;
            case R.id.ll_mine:
                selectItem(1);
                break;
        }
    }
}
